***************************************************************************************************
* This do file run OLS(DID) analysis on firm-product level, and output all the tables and figures
* Outputs: Figure 2, Table 4, Table 5, Table 6, Table 7, Table 8, Table A4, Table A6, Table A7,
* Table A8 and Table A9.
* Last modified by: Yukun Wang
* Date: 08/10/2018
***************************************************************************************************

clear all
set more off
set scheme s1color
set matsize 11000

version

***Set directory
capture cd "/Users/Dropbox/Chinese food exports"


***load data
use "Data/Coded data/Customs/customs_yearly_2000_2013_firmproduct_level_dairy_dropban0.dta", clear

drop if interm==1 //drop all the intermediaries


label var firmXproductsXpost "CFirm-ProductXPost"
label var firmXpost "CFirmXPost"
label var productsXpost "CProductXPost"
label var ifirmXproductsXpost "IFirm-ProductXPost"
label var ifirmXpost "IFirmXPost"

label var sourceprovXproductsXpost "CSourceProv-ProductXPost"
label var sourceprovXpost "CSourceProvXPost"
label var sourcecityXproductsXpost "CSourceCity-ProductXPost"
label var sourcecityXpost "CSourceCityXPost"
label var subfirmXpost "Subsidiary of CfirmXPost"
label var intensityfirmXpost "Intensity of CFirmXPost"
label var intensityproductsXpost "Intensity of CProductXPost"

drop hs2digit
g hs2digit=substr(hs_id,1,2)
egen hs2_num=group(hs2digit)

g hs4digit=substr(hs_id,1,4)
egen hs4_num=group(hs4digit)

egen hs8_num=group(hs_id)


*****************
*    Table 4    *
*****************

foreach var of varlist ihsvalue ihsquantity lprice entry {

//firm-product and prov-year FE
reghdfe `var' firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost c.firm_sales_bl#1.post, absorb(product_firm year) vce(cluster product_year party)
est sto `var'_main1_inn

}


***Output table

tempname texfile
file open `texfile' using "Results/main_tables/table_4.tex", write replace
file close `texfile' 

estout ihsvalue_main1_inn ihsquantity_main1_inn lprice_main1_inn entry_main1_inn using "Results/main_tables/table_4.tex", cells(b(star fmt(%9.3f)) se(par)) ///
	keep(firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost) ///
	starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2 N, fmt(3 %9.0f) labels(R-squared Observations)) mlabels(,none) ///
	label style(tex) collabels(none) eqlabels(none) append 
	
	
*****************
*    Figure 2   *
*****************

foreach var of varlist ihsvalue entry{

	*all firms
	reghdfe `var' firmXyear2 productsXyear2 firmXproductsXyear2 ifirmXyear2 ifirmXproductsXyear2 ///
				firmXyear3 productsXyear3 firmXproductsXyear3 ifirmXyear3 ifirmXproductsXyear3 ///
				firmXyear4 productsXyear4 firmXproductsXyear4 ifirmXyear4 ifirmXproductsXyear4 ///
				firmXyear5 productsXyear5 firmXproductsXyear5 ifirmXyear5 ifirmXproductsXyear5 ///
				firmXyear6 productsXyear6 firmXproductsXyear6 ifirmXyear6 ifirmXproductsXyear6 ///
				firmXyear7 productsXyear7 firmXproductsXyear7 ifirmXyear7 ifirmXproductsXyear7 ///
				firmXyear8 productsXyear8 firmXproductsXyear8 ifirmXyear8 ifirmXproductsXyear8 ///
				firmXyear9 productsXyear9 firmXproductsXyear9 ifirmXyear9 ifirmXproductsXyear9 ///
				firmXyear10 productsXyear10 firmXproductsXyear10 ifirmXyear10 ifirmXproductsXyear10 ///	
				firmXyear11 productsXyear11 firmXproductsXyear11 ifirmXyear11 ifirmXproductsXyear11 ///
				firmXyear12 productsXyear12 firmXproductsXyear12 ifirmXyear12 ifirmXproductsXyear12 ///
				firmXyear13 productsXyear13 firmXproductsXyear13 ifirmXyear13 ifirmXproductsXyear13 ///
				firmXyear14 productsXyear14 firmXproductsXyear14 ifirmXyear14 ifirmXproductsXyear14 c.firm_sales_bl#1.post, absorb(product_firm year) vce(cluster product_year party)

	mat R=r(table)

	matselrc R R1, r(1/2) c(1,6,11,16,21,26,31,36,41,46,51,56,61)
	matselrc R R2, r(1/2) c(2,7,12,17,22,27,32,37,42,47,52,57,62)
	matselrc R R3, r(1/2) c(3,8,13,18,23,28,33,38,43,48,53,58,63)
	matselrc R R4, r(1/2) c(4,9,14,19,24,29,34,39,44,49,54,59,64)
	matselrc R R5, r(1/2) c(5,10,15,20,25,30,35,40,45,50,55,60,65)

	mat t=(2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013)

	mat S=R1',R2',R3',R4',R5',t'

	svmat S

	forvalues i=1(2)9{
	generate S`i'_high = S`i' + 1.96*S`=`i'+1'
	generate S`i'_low = S`i' - 1.96*S`=`i'+1'
	}			

graph twoway (scatter S1 S11,sort connect(l) lw(medthick) col(blue) msymbol(diamond) msize(medlarge) xline(2008.5,lp(dash) lw(thick) lc(navy)) yaxis(1) ytitle("Within-firm spillovers",axis(1) size(large)) legend(off)) ///
			 (rcap S1_high S1_low S11,sort lc(blue) lp(shortdash_dot) legend(off)), ///
			 xlabel(2008.5 "Event",labs(medium) labgap(2)) xmlabel(2001(1)2013, labsize(3.5)) xscale(r(2001(1)2013)) ///
			 xtitle("Year") scale(1) graphregion(color(white)) bgcolor(white) aspectratio(0.5) ylabel(, labsize(med)) xlabel(, labsize(medium))
			 *legend(order(1) cols(1) label(1 "CFirmsXYear") ring(1) pos(6) )
			 
graph export "Results/main_figures/figure_2_`var'_2.pdf",replace


graph twoway (scatter S5 S11,sort connect(l) lw(medthick) col(red) msymbol(square) msize(medlarge) xline(2008.5,lp(dash) lw(thick) lc(navy)) yaxis(1) ytitle("Direct effect", axis(1) size(vlarge)) legend(off) ) ///
			 (rcap S5_high S5_low S11,sort lc(red) lp(shortdash_dot) legend(off) ), ///
			 xlabel(2008.5 "Event",labs(medium) labgap(2)) xmlabel(2001(1)2013, labsize(3.5)) xscale(r(2001(1)2013)) ///
			 xtitle("Year") scale(1) graphregion(color(white)) bgcolor(white) aspectratio(0.5) ylabel(, labsize(med)) xlabel(, labsize(medium))		
			 *legend(order(1) cols(1) label(1 "CFirmsProductsXYear") ring(1) pos(6) )

graph export "Results/main_figures/figure_2_`var'_1.pdf",replace
	


graph twoway (scatter S3 S11,sort connect(l) lw(medthick) col(green) msymbol(triangle) msize(medlarge) xline(2008.5,lp(dash) lw(thick) lc(navy)) yaxis(1) ytitle("Across-products spillovers", axis(1) size(large)) legend(off)) ///
			 (rcap S3_high S3_low S11,sort lc(green) lp(shortdash_dot) legend(off)), ///
			 xlabel(2008.5 "Event",labs(medium) labgap(2)) xmlabel(2001(1)2013, labsize(3.5)) xscale(r(2001(1)2013)) ///
			 xtitle("Year") scale(1) graphregion(color(white)) bgcolor(white) aspectratio(0.5) ylabel(, labsize(med)) xlabel(, labsize(medium))
			 *legend(order(1) cols(1) label(1 "CProductsXYear") ring(1) pos(6) )
graph export "Results/main_figures/figure_2_`var'_3.pdf",replace


drop S1-S9_low

}



****************
*    Table 6   *
****************


foreach var of varlist ihsgnewsindex_value2 ihsgnewsindex_value1 ihsgnewsindex_value0 ///
                       ihsgnewsindex_quantity2 ihsgnewsindex_quantity1 ihsgnewsindex_quantity0 ///
					   lgnewsindex_price2 lgnewsindex_price1 lgnewsindex_price0 ///
					   entry_gnewsindex_value2 entry_gnewsindex_value1 entry_gnewsindex_value0{

reghdfe `var' firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost c.firm_sales_bl#1.post, absorb(product_firm year) vce(cluster product_year party)
est sto `var'

}

tempname texfile
file open `texfile' using "Results/main_tables/table_6.tex", write replace
file close `texfile' 

estout ihsgnewsindex_value2 ihsgnewsindex_value1 ihsgnewsindex_value0 ///
       ihsgnewsindex_quantity2 ihsgnewsindex_quantity1 ihsgnewsindex_quantity0 ///
	   entry_gnewsindex_value2 entry_gnewsindex_value1 entry_gnewsindex_value0 ///
	   using "Results/main_tables/table_6.tex", cells(b(star fmt(%9.3f)) se(par)) ///
	   keep(firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost) ///
	   starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2 N, fmt(3 %9.0f) labels(R-squared Observations)) mlabels(,none) ///
	   label style(tex) collabels(none) eqlabels(none) append 



****************
*    Table 7   *
****************

foreach var of varlist ihsvalue ihsquantity lprice entry {

reghdfe `var' productsXpost sourcecityXproductsXpost sourcecityXpost c.firm_sales_bl#1.post if affected_firms==0, absorb(product_firm year) vce(cluster product_year party)
est sto `var'_city_any

}



tempname texfile
file open `texfile' using "Results/main_tables/table_7.tex", write replace
file close `texfile' 

estout ihsvalue_city_any ihsquantity_city_any lprice_city_any entry_city_any ///
	using "Results/main_tables/table_7.tex", cells(b(star fmt(%9.3f)) se(par)) ///
	keep(productsXpost sourcecityXproductsXpost sourcecityXpost) ///
	starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2 N, fmt(3 %9.0f) labels(R-squared Observations)) mlabels(,none) ///
	label style(tex) collabels(none) eqlabels(none) append 

	
	
****************
*    Table 8   *
****************

	
foreach var of varlist ihsvalue ihsquantity lprice entry {
*years of exporting experience

reghdfe `var' firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost c.firm_sales_bl#1.post ///
i.hs2_num#c.year if experienced==1, absorb(product_firm year) vce(cluster product_year party)
est sto `var'_old1

reghdfe `var' firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost c.firm_sales_bl#1.post ///
i.hs2_num#c.year if experienced==0, absorb(product_firm year) vce(cluster product_year party)
est sto `var'_new1


reghdfe `var' firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost c.firm_sales_bl#1.post ///
i.hs2_num#1.post if experienced==1, absorb(product_firm year) vce(cluster product_year party)
est sto `var'_old2

reghdfe `var' firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost c.firm_sales_bl#1.post ///
i.hs2_num#1.post if experienced==0, absorb(product_firm year) vce(cluster product_year party)
est sto `var'_new2


}


tempname texfile
file open `texfile' using "Results/main_tables/table_8.tex", write replace
file close `texfile' 

estout ihsvalue_old1 ihsvalue_new1 ihsvalue_old2  ihsvalue_new2 ///
	entry_old1 entry_new1 entry_old2 entry_new2 ///
	using "Results/main_tables/table_8.tex", cells(b(star fmt(%9.3f)) se(par)) ///
	keep(firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost) ///
	starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2 N, fmt(3 %9.0f) labels(R-squared Observations)) mlabels(,none) ///
	label style(tex) collabels(none) eqlabels(none) append 



	
	

	
**************************
*    Table A4 Panel A    *
**************************
	
foreach var of varlist ihsvalue ihsquantity lprice entry {

//firm, product and prov-year FE
reghdfe `var' firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost c.firm_sales_bl#1.post, absorb(hs8_num party prov_year) vce(cluster product_year party)
est sto `var'_main2_inn

}


***Output table

tempname texfile
file open `texfile' using "Results/appendix_tables/table_a4_panel_a.tex", write replace
file close `texfile' 

estout ihsvalue_main2_inn ihsquantity_main2_inn lprice_main2_inn entry_main2_inn using "Results/appendix_tables/table_a4_panel_a.tex", cells(b(star fmt(%9.3f)) se(par)) ///
	keep(firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost) ///
	starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2 N, fmt(3 %9.0f) labels(R-squared Observations)) mlabels(,none) ///
	label style(tex) collabels(none) eqlabels(none) append 
	
	
	
**************************
*    Table A4 Panel B    *
**************************

foreach var of varlist ihsvalue ihsquantity lprice entry {

reghdfe `var' firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost  ///
c.firm_sales_bl#1.post ///
i.hs2_num#c.year , absorb(product_firm year) vce(cluster product_year party)
est sto `var'_trend1

}

***Output tables

tempname texfile
file open `texfile' using "Results/appendix_tables/table_a4_panel_b.tex", write replace
file close `texfile' 

estout ihsvalue_trend1 ihsquantity_trend1 lprice_trend1 entry_trend1 using "Results/appendix_tables/table_a4_panel_b.tex", cells(b(star fmt(%9.3f)) se(par)) ///
	keep(firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost) ///
	starlevels( * 0.10 ** 0.05 *** 0.010) stats(N, fmt(%9.0f) labels(Observations)) mlabels(,none) ///
	label style(tex) collabels(none) eqlabels(none) append 
	
	
**************************
*    Table A4 Panel C    *
**************************


preserve
drop if nonexp07==1
foreach var of varlist ihsvalue ihsquantity lprice entry {

//firm-product and year FE
reghdfe `var' firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost firmspecdemandshock c.firm_sales_bl#1.post, absorb(product_firm year) vce(cluster product_year party)
est sto `var'_main1_rob
}

tempname texfile
file open `texfile' using "Results/appendix_tables/table_a4_panel_c.tex", write replace
file close `texfile' 

estout ihsvalue_main1_rob ihsquantity_main1_rob lprice_main1_rob entry_main1_rob  ///
	using "Results/appendix_tables/table_a4_panel_c.tex", cells(b(star fmt(%9.3f)) se(par)) ///
	keep(firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost) ///
	starlevels( * 0.10 ** 0.05 *** 0.010) stats(N, fmt(%9.0f) labels(Observations)) mlabels(,none) ///
	label style(tex) collabels(none) eqlabels(none) append 
	
restore


***Keep main results and merge later
keep _est_ihsvalue_main1_inn _est_ihsquantity_main1_inn _est_lprice_main1_inn _est_entry_main1_inn

tempfile main_results
save `main_results', replace


******************
*    Table A6    *
******************

***Drop driving firms
use "Data/Coded data/Customs/customs_yearly_2000_2013_firmproduct_level_dairy_dropban0.dta", clear	

drop if driving_firms==1
drop if interm==1 //drop all the intermediaries

label var firmXproductsXpost "CFirm-ProductXPost"
label var firmXpost "CFirmXPost"
label var productsXpost "CProductXPost"
label var ifirmXproductsXpost "IFirm-ProductXPost"
label var ifirmXpost "IFirmXPost"

foreach var of varlist ihsvalue ihsquantity lprice entry {

reghdfe `var' firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost c.firm_sales_bl#1.post, absorb(product_firm year) vce(cluster product_year party)
est sto `var'_dropdfirms

}


keep _est_ihsvalue_dropdfirms _est_ihsquantity_dropdfirms _est_lprice_dropdfirms _est_entry_dropdfirms

tempfile dropdfirms_results
save `dropdfirms_results', replace


***Drop driving destinations
use "Data/Coded data/Customs/customs_yearly_2000_2013_firmproduct_level_dairy_dropspikectry1.dta", clear	
merge m:1 party_id year using "Data/firm_codebook/company_codebook.dta"
drop if _m==2
drop _m
g interm = 1 if ustrpos(company,"进出口") != 0 | ustrpos(company,"经贸") != 0 | ustrpos(company,"贸易") != 0 | ustrpos(company,"科贸") != 0 | ustrpos(company,"外经") != 0
replace interm = 0 if interm==. & company!=""

drop if interm==1 //drop all the intermediaries

label var firmXproductsXpost "CFirm-ProductXPost"
label var firmXpost "CFirmXPost"
label var productsXpost "CProductXPost"
label var ifirmXproductsXpost "IFirm-ProductXPost"
label var ifirmXpost "IFirmXPost"


egen prov_year=group(firmprov year)
foreach var of varlist ihsvalue ihsquantity lprice entry {

reghdfe `var' firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost c.firm_sales_bl#1.post, absorb(product_firm year) vce(cluster product_year party)
est sto `var'_dctrys

}



keep _est_ihsvalue_dctrys _est_ihsquantity_dctrys _est_lprice_dctrys _est_entry_dctrys

tempfile dctrys_results
save `dctrys_results', replace
	
***Drop driving firms and destinations
use "Data/Coded data/Customs/customs_yearly_2000_2013_firmproduct_level_dairy_dropspikectry1.dta", clear	

merge m:1 party_id year using "Data/firm_codebook/company_codebook.dta"
drop if _m==2
drop _m
g interm = 1 if ustrpos(company,"进出口") != 0 | ustrpos(company,"经贸") != 0 | ustrpos(company,"贸易") != 0 | ustrpos(company,"科贸") != 0 | ustrpos(company,"外经") != 0
replace interm = 0 if interm==. & company!=""


drop if interm==1 //drop all the intermediaries


egen prov_year=group(firmprov year)
drop if driving_firms==1

foreach var of varlist ihsvalue ihsquantity lprice entry {

reghdfe `var' firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost c.firm_sales_bl#1.post, absorb(product_firm year) vce(cluster product_year party)
est sto `var'_dfirmctrys

}


***Combine all the results
append using `main_results'
append using `dropdfirms_results'
append using `dctrys_results'

label var firmXproductsXpost "CFirm-ProductXPost"
label var firmXpost "CFirmXPost"
label var productsXpost "CProductXPost"
label var ifirmXproductsXpost "IFirm-ProductXPost"
label var ifirmXpost "IFirmXPost"

estout ihsvalue_dropdfirms ihsvalue_dctrys ihsvalue_dfirmctrys ihsquantity_dropdfirms ihsquantity_dctrys ihsquantity_dfirmctrys entry_dropdfirms entry_dctrys entry_dfirmctrys using "Results/appendix_tables/table_a6.tex", cells(b(star fmt(%9.3f)) se(par)) ///
	keep(firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost) ///
	starlevels( * 0.10 ** 0.05 *** 0.010) stats(N, fmt(%9.0f) labels(Observations)) mlabels(,none) ///
	label style(tex) collabels(none) eqlabels(none) append 



	

******************
*    Table A7    *
******************

use "Data/Coded data/Customs/customs_yearly_2000_2013_firmproduct_level_dairy_dropban1.dta", clear

drop if interm==1 //drop all the intermediaries

label var firmXproductsXpost "CFirm-ProductXPost"
label var firmXpost "CFirmXPost"
label var productsXpost "CProductXPost"
label var ifirmXproductsXpost "IFirm-ProductXPost"
label var ifirmXpost "IFirmXPost"

foreach var of varlist ihsvalue ihsquantity lprice entry {

reghdfe `var' firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost c.firm_sales_bl#1.post, absorb(product_firm year) vce(cluster product_year party)
est sto `var'_dropban_trend1

}


tempname texfile
file open `texfile' using "Results/appendix_tables/table_a7.tex", write replace
file close `texfile' 

append using `main_results'

label var firmXproductsXpost "CFirm-ProductXPost"
label var firmXpost "CFirmXPost"
label var productsXpost "CProductXPost"
label var ifirmXproductsXpost "IFirm-ProductXPost"
label var ifirmXpost "IFirmXPost"

estout ihsvalue_dropban_trend1 ihsvalue_main1_inn ihsquantity_dropban_trend1 ihsquantity_main1_inn lprice_dropban_trend1 lprice_main1_inn entry_dropban_trend1 entry_main1_inn using "Results/appendix_tables/table_a7.tex", cells(b(star fmt(%9.3f)) se(par)) ///
	keep(firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost) ///
	starlevels( * 0.10 ** 0.05 *** 0.010) stats(N, fmt(%9.0f) labels(Observations)) mlabels(,none) ///
	label style(tex) collabels(none) eqlabels(none) append 



	
	
******************
*    Table A8    *
******************	
	
use "Data/Coded data/Customs/customs_yearly_2000_2013_firmproduct_level_dairy_step1", clear

drop gwebindex gnewsindex

*** Merge in new GTrends data
merge m:1 origin_id using "Data/google_trends/gtrends1.dta" //here use the new gtrends data
tab _m
drop if _m==2
drop _m
	
save "Data/Coded data/Customs/customs_yearly_2000_2013_firmproduct_level_dairy_step1", replace


do "Codes/0_data_prep/customs_firm_product_data_prep2.do" //then could use customs_yearly_2000_2013_firmproduct_level_dairy_dropban0/1.dta for analysis


use "Data/Coded data/Customs/customs_yearly_2000_2013_firmproduct_level_dairy_dropban0.dta", clear

drop if interm==1

label var firmXproductsXpost "CFirm-ProductXPost"
label var firmXpost "CFirmXPost"
label var productsXpost "CProductXPost"
label var ifirmXproductsXpost "IFirm-ProductXPost"
label var ifirmXpost "IFirmXPost"

label var sourceprovXproductsXpost "CSourceProv-ProductXPost"
label var sourceprovXpost "CSourceProvXPost"
label var sourcecityXproductsXpost "CSourceCity-ProductXPost"
label var sourcecityXpost "CSourceCityXPost"
label var subfirmXpost "Subsidiary of CfirmXPost"
label var intensityfirmXpost "Intensity of CFirmXPost"
label var intensityproductsXpost "Intensity of CProductXPost"

drop hs2digit
g hs2digit=substr(hs_id,1,2)
egen hs2_num=group(hs2digit)

g hs4digit=substr(hs_id,1,4)
egen hs4_num=group(hs4digit)

egen hs8_num=group(hs_id)



foreach var of varlist ihsgnewsindex_value3 ihsgnewsindex_value2 ihsgnewsindex_value1 ihsgnewsindex_value0 ///
                       ihsgnewsindex_quantity3 ihsgnewsindex_quantity2 ihsgnewsindex_quantity1 ihsgnewsindex_quantity0 ///
					   lgnewsindex_price3 lgnewsindex_price2 lgnewsindex_price1 lgnewsindex_price0 ///
					   entry_gnewsindex_value3 entry_gnewsindex_value2 entry_gnewsindex_value1 entry_gnewsindex_value0{

reghdfe `var' firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost c.firm_sales_bl#1.post, absorb(product_firm year) vce(cluster product_year party)
est sto `var'

}

tempname texfile
file open `texfile' using "Results/appendix_tables/table_a8.tex", write replace
file close `texfile' 

estout ihsgnewsindex_value3 ihsgnewsindex_value2 ihsgnewsindex_value1 ihsgnewsindex_value0 ///
       ihsgnewsindex_quantity3 ihsgnewsindex_quantity2 ihsgnewsindex_quantity1 ihsgnewsindex_quantity0 ///
	   entry_gnewsindex_value3 entry_gnewsindex_value2 entry_gnewsindex_value1 entry_gnewsindex_value0 ///
	   using "Results/appendix_tables/table_a8.tex", cells(b(star fmt(%9.3f)) se(par)) ///
	   keep(firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost) ///
	   starlevels( * 0.10 ** 0.05 *** 0.010) stats(N, fmt(%9.0f) labels(Observations)) mlabels(,none) ///
	   label style(tex) collabels(none) eqlabels(none) append 



******************
*    Table A9    *
******************

use "Data/Coded data/Customs/customs_yearly_2000_2013_firmproduct_level_dairy_dropban0.dta", clear

drop if interm==1 //drop all the intermediaries

label var firmXproductsXpost "CFirm-ProductXPost"
label var firmXpost "CFirmXPost"
label var productsXpost "CProductXPost"
label var ifirmXproductsXpost "IFirm-ProductXPost"
label var ifirmXpost "IFirmXPost"

label var sourceprovXproductsXpost "CSourceProv-ProductXPost"
label var sourceprovXpost "CSourceProvXPost"
label var sourcecityXproductsXpost "CSourceCity-ProductXPost"
label var sourcecityXpost "CSourceCityXPost"
label var subfirmXpost "Subsidiary of CfirmXPost"
label var intensityfirmXpost "Intensity of CFirmXPost"
label var intensityproductsXpost "Intensity of CProductXPost"

drop hs2digit
g hs2digit=substr(hs_id,1,2)
egen hs2_num=group(hs2digit)

g hs4digit=substr(hs_id,1,4)
egen hs4_num=group(hs4digit)

egen hs8_num=group(hs_id)

merge m:1 party_id using "Data/firm_codebook/vs_new_old.dta"
drop if _m==2
drop _m


foreach var of varlist ihsvalue ihsquantity lprice entry {
*years of exporting experience

reghdfe `var' firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost c.firm_sales_bl#1.post ///
i.hs2_num#c.year if established_manu==1, absorb(product_firm year) vce(cluster product_year party)
est sto `var'_estab1_m

reghdfe `var' firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost c.firm_sales_bl#1.post ///
i.hs2_num#c.year if established_manu==0, absorb(product_firm year) vce(cluster product_year party)
est sto `var'_entry1_m


reghdfe `var' firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost c.firm_sales_bl#1.post ///
i.hs2_num#1.post if established_manu==1, absorb(product_firm year) vce(cluster product_year party)
est sto `var'_estab2_m

reghdfe `var' firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost c.firm_sales_bl#1.post ///
i.hs2_num#1.post if established_manu==0, absorb(product_firm year) vce(cluster product_year party)
est sto `var'_entry2_m


}


tempname texfile
file open `texfile' using "Results/appendix_tables/table_a9.tex", write replace
file close `texfile' 

estout ihsvalue_estab1_m ihsvalue_entry1_m ihsvalue_estab2_m  ihsvalue_entry2_m ///
	entry_estab1_m entry_entry1_m entry_estab2_m entry_entry2_m ///
	using "Results/appendix_tables/table_a9.tex", cells(b(star fmt(%9.3f)) se(par)) ///
	keep(firmXproductsXpost firmXpost productsXpost ifirmXproductsXpost ifirmXpost) ///
	starlevels( * 0.10 ** 0.05 *** 0.010) stats(N, fmt(%9.0f) labels(Observations)) mlabels(,none) ///
	label style(tex) collabels(none) eqlabels(none) append 

	
	
******************
*    Table 5    *
******************	
	
use "Data/Coded data/manucensus_coded",clear

keep if inrange(year,2005,2013)
drop if year==2010

keep if dairy==1

bys panelid: gen N=_N
keep if N==8
drop N

	
reghdfe lnsalesrevenue affectedXpost innocentXpost c.firm_sales_bl#1.post, absorb(panelid year) vce(cluster panelid)
est store sales_main1_bal1

reghdfe lndsalesrevenue affectedXpost innocentXpost c.firm_sales_bl#1.post, absorb(panelid year) vce(cluster panelid)
est store dsales_main1_bal1

reghdfe lnemployment affectedXpost innocentXpost c.firm_sales_bl#1.post, absorb(panelid year) vce(cluster panelid)
est store employment_main1_bal1

tempname texfile
file open `texfile' using "Results/main_tables/table_5.tex", write replace
file close `texfile' 

estout sales_main1_bal1 dsales_main1_bal1 employment_main1_bal1 using "Results/main_tables/table_5.tex", cells(b(star fmt(%9.3f)) se(par)) ///
	keep(affectedXpost innocentXpost) ///
	starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2 N, fmt(3 %9.0f) labels(R-squared Observations)) mlabels(,none) ///
	label style(tex) collabels(none) eqlabels(none) append 
